package edu.ynu.dao;
// default package

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import edu.ynu.po.ChargePerson;

/**
 	* A data access object (DAO) providing persistence and search support for ChargePerson entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .ChargePerson
  * @author MyEclipse Persistence Tools 
 */

public class ChargePersonDAO extends BaseHibernateDAO  {
		 private static final Log log = LogFactory.getLog(ChargePersonDAO.class);
		//property constants
	public static final String NAME = "name";
	public static final String SEX = "sex";
	public static final String PHONE = "phone";
	public static final String EMAIL = "email";
	public static final String POSITION_ID = "positionId";
	public static final String DEPT_ID = "deptId";
	public static final String CREATETIME = "createtime";
	public static final String REMARK = "remark";



    
    public void save(ChargePerson transientInstance) {
        log.debug("saving ChargePerson instance");
        try {
        	Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.save(transientInstance);
			tx.commit();
			closeSession();
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(ChargePerson persistentInstance) {
        log.debug("deleting ChargePerson instance");
        try {
        	Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.delete(persistentInstance);
			tx.commit();
			closeSession();
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
	
	public void update(ChargePerson persistentInstance) {
		log.debug("deleting Candidate instance");
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.update(persistentInstance);
			tx.commit();
			closeSession();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
    
    public ChargePerson findById( java.lang.Integer id) {
        log.debug("getting ChargePerson instance with id: " + id);
        try {
            ChargePerson instance = (ChargePerson) getSession()
                    .get("edu.ynu.po.ChargePerson", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(ChargePerson instance) {
        log.debug("finding ChargePerson instance by example");
        try {
            List results = getSession()
                    .createCriteria("edu.ynu.po.ChargePerson")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding ChargePerson instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from ChargePerson as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByName(Object name
	) {
		return findByProperty(NAME, name
		);
	}
	
	public List findBySex(Object sex
	) {
		return findByProperty(SEX, sex
		);
	}
	
	public List findByPhone(Object phone
	) {
		return findByProperty(PHONE, phone
		);
	}
	
	public List findByEmail(Object email
	) {
		return findByProperty(EMAIL, email
		);
	}
	
	public List findByPositionId(Object positionId
	) {
		return findByProperty(POSITION_ID, positionId
		);
	}
	
	public List findByDeptId(Object deptId
	) {
		return findByProperty(DEPT_ID, deptId
		);
	}
	
	public List findByCreatetime(Object createtime
	) {
		return findByProperty(CREATETIME, createtime
		);
	}
	
	public List findByRemark(Object remark
	) {
		return findByProperty(REMARK, remark
		);
	}
	

	public List findAll() {
		log.debug("finding all ChargePerson instances");
		try {
			String queryString = "from ChargePerson";
	         Query queryObject = getSession().createQuery(queryString);
			 return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public ChargePerson merge(ChargePerson detachedInstance) {
        log.debug("merging ChargePerson instance");
        try {
            ChargePerson result = (ChargePerson) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(ChargePerson instance) {
        log.debug("attaching dirty ChargePerson instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(ChargePerson instance) {
        log.debug("attaching clean ChargePerson instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}